<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\tool\Result;
use think\facade\View;
use think\Response;
use think\response\Json;
use app\common\model\Draw as DrawModel;
use think\exception\ValidateException;

class Draw extends Base
{

    public function index()
    {
        $account = [];
        foreach (['bonus'] as $item) {
            $account[] = [
                'title'   => get_pay_way($item),
                'type'    => $item,
                'balance' => $this->user->assets->$item,
            ];
        }
        return View::fetch('', [
            'account' => $account
        ]);
    }

    public function mode()
    {
        return View::fetch();
    }

    public function record()
    {
        return View::fetch('', [
            'tab' => ['' => '全部', 1 => '待审核', 2 => '已通过', 3 => '已驳回'],
        ]);
    }

    public function getList(): Response
    {
        $param = $this->request->get();
        $map = ['uid' => $this->user->id];
        if (!empty($param['type'])) {
            $map['status'] = $param['type'];
        }
        return Result::fastReturn(DrawModel::getPageList($map, '*', $param['page'], 20, false,
            '', '', '', '', '', '', ['statusText']));
    }

    public function save(): Json
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            $this->validate($param, 'Draw');
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
        return json(DrawModel::add($param, $this->user, $this->system));
    }

}

